package main

import (
	"./collect"
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"gopkg.in/ini.v1"
	"log"
	"regexp"
)

var db *sql.DB
var cfg *ini.File

const CFG_DATA = `
[collect]
file_path=/var/log/nginx/access.log.2.gz
need_unzip=true

[db]
dblink=ydl:tt12345@tcp(ydldbhost:3306)/ydl?charset=utf8
`

func main() {
	collectSec, _ := cfg.GetSection("collect")
	needUnzip, _ := collectSec.Key("need_unzip").Bool()
	c := collect.NewCollect(collectSec.Key("file_path").String(), needUnzip)
	reg := regexp.MustCompile("m\\.yidianling\\.com")

	c.OnItem(func(item *collect.Item) {
		if reg.MatchString(item.Host) && item.Status == 404 {
			//插入数据
			stmt, err := db.Prepare("INSERT site_access_record SET ip=?,access_time=?,method=?,url=?,status_code=?,body_size=?,proto=?,referer=?,user_agent=?")
			checkErr(err)

			res, err := stmt.Exec(
				item.Ip,
				item.Time,
				item.Method,
				item.Url,
				item.Status,
				item.BodySize,
				item.Proto,
				item.Referer,
				item.UserAgent)
			fmt.Println(res)
		}

	})

	err := c.GetItems()
	fmt.Println(err)
	c.Exit()

}

func checkErr(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

func init() {
	var err error
	cfg, _ = ini.Load([]byte(CFG_DATA), "./logana.ini")
	dbSec, _ := cfg.GetSection("db")

	db, err = sql.Open("mysql", dbSec.Key("dblink").String())
	if err != nil {
		log.Fatal(err)
	}
}
